﻿package com.bn.NNYY;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ScoreSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
	MyActivity activity;//activity的引用
	Paint paint;//画笔引用
	Bitmap highScoreBitmap;//文字的图片
	Bitmap defenBitmap;
	Bitmap riqiBitmap;
	Bitmap[] numberBitmaps;//数字图片	
	Bitmap gangBitmap;//符号"-"对应的图片
	Bitmap background;
	int bmpx;//文字位置	
	String queryResultStr;//查询数据库的结果
	String[] splitResultStrs;//将查询结果切分后的数组
	private int numberWidth;//数字图片的宽度
	private int posFrom=-1;//查询的开始位置
	private int length=6;//查询的最大记录个数
	int downY=0;//按下和抬起的y坐标
	int upY=0;
	DrawThread drawThread;

	public ScoreSurfaceView(MyActivity activity) {
		super(activity);
		this.activity=activity;
		//获得焦点并设置为可触控
		this.requestFocus();
        this.setFocusableInTouchMode(true);
		getHolder().addCallback(this);//注册回调接口
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//绘制背景
		canvas.drawColor(Color.WHITE);
		//绘制背景图片
		canvas.drawBitmap(background, 0, 0, paint);
		//绘制文字
		canvas.drawBitmap(highScoreBitmap, bmpx, 5, paint);
		canvas.drawBitmap(defenBitmap, Constant.SCREEN_WIDTH*2/3, highScoreBitmap.getHeight()+30, paint);
		canvas.drawBitmap(riqiBitmap, Constant.SCREEN_WIDTH/3, highScoreBitmap.getHeight()+30, paint);
		//绘制得分和时间
		int x,y;
		for(int i=0;i<splitResultStrs.length;i++)
		{
			if(i%2==0)//计算得分的位置
			{
				x=Constant.SCREEN_WIDTH*3/4;				
			}
			else//计算时间的位置
			{
				x=Constant.SCREEN_WIDTH/2;
			}
			y=20+highScoreBitmap.getHeight()+defenBitmap.getHeight()+(numberBitmaps[0].getHeight()+10)*(i/2+1);
			//绘制字符串
			drawDateBitmap(splitResultStrs[i],x,y,canvas,paint);
		}
	}
	//画日期图片的方法
	public void drawDateBitmap(String numberStr,float endX,float endY,Canvas canvas,Paint paint)
	{
		for(int i=0;i<numberStr.length();i++)
		{
			char c=numberStr.charAt(i);
			if(c=='-')
			{
				canvas.drawBitmap(gangBitmap,endX-numberWidth*(numberStr.length()-i), endY, paint);
			}
			else
			{
				canvas.drawBitmap
				(
						numberBitmaps[c-'0'], 
						endX-numberWidth*(numberStr.length()-i), 
						endY, 
						paint
				);
			}			
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int y = (int) event.getY();		
    	switch(event.getAction())
    	{
    	case MotionEvent.ACTION_DOWN:
    		downY=y;
    		break;
    	case MotionEvent.ACTION_UP:
    		upY=y;    		
        	if(Math.abs(downY-upY)<20)//在域值范围内，不翻页
        	{
        		return true;
        	}
        	else if(downY<upY)//往下抹
        	{	
        		//如果抹到最前页，不可再抹
        		if(this.posFrom-this.length>=-1)
        		{
        			this.posFrom-=this.length;        			
        		}
        	}
        	else//往上抹
        	{	
        		//如果抹到最后页，不可再抹
        		if(this.posFrom+this.length<DBUtils.getRowCount()-1)
        		{
        			this.posFrom+=this.length;        			
        		}
        	}
        	queryResultStr=DBUtils.query(posFrom,length);//得到数据库中的数据
			splitResultStrs=queryResultStr.split("/", 0);//用"/"切分，且舍掉空串
    		break;    	
    	}
		return true;
	}	

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		paint=new Paint();//创建画笔
		paint.setAntiAlias(true);//打开抗锯齿
		createAllThreads();
		initBitmap();//初始化位图资源
		numberWidth=numberBitmaps[0].getWidth()+3;//得到数字图片的宽度
		bmpx=(Constant.SCREEN_WIDTH-highScoreBitmap.getWidth())/2;
		posFrom=-1;
		queryResultStr=DBUtils.query(posFrom,length);//得到数据库中的数据
		splitResultStrs=queryResultStr.split("/", 0);//用"/"切分，且舍掉空串	
		startAllThreads();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	public void initBitmap(){
		highScoreBitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.highscore);
		numberBitmaps=new Bitmap[]{
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n0),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n1),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n2),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n3),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n4),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n5),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n6),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n7),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n8),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n9),
				BitmapFactory.decodeResource(this.getResources(), R.drawable.n0),
		};
		gangBitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.gang);
		defenBitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.defen1);
		riqiBitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.riqi);
		//主界面的背景图片
		background=BitmapFactory.decodeResource(this.getResources(), R.drawable.main_background);
		background=PicLoadUtil.scaleToFit(background,Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		
	}
    void createAllThreads()
	{
		drawThread=new DrawThread(this);//创建绘制线程		
	}
	void startAllThreads()
	{
		drawThread.setFlag(true);     
		drawThread.start();
	}
	void stopAllThreads()
	{
		drawThread.setFlag(false);       
	}
	
	public void repaint()
	{
		Canvas canvas=this.getHolder().lockCanvas();
		try
		{
			synchronized(canvas)
			{
				onDraw(canvas);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(canvas!=null)
			{
				this.getHolder().unlockCanvasAndPost(canvas);
			}
		}
	}
	private class DrawThread extends Thread{
		private boolean flag = true;	
		private int sleepSpan = 100;
		public DrawThread(ScoreSurfaceView fatherView){
		}
		public void run(){
	        while (this.flag) {
	            try{
	            	Thread.sleep(sleepSpan);//睡眠指定毫秒数
	            }
	            catch(Exception e){
	            	e.printStackTrace();//打印堆栈信息
	            }
	            repaint();
	        }
		}
		public void setFlag(boolean flag) {
			this.flag = flag;
		}
	}

}
